﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Dapper;
using LibMS.Models;

namespace LibMS.Controllers
{
    [Authorize]
    public class LibDocumentsController : Controller
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [AllowAnonymous]
        public ActionResult Index(string order)
        {
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@PageSize", 5);
            parameters.Add("@PageIndex", 1);
            parameters.Add("@OrderBy", order);
            var data = _unitOfWork.LibDocuments.GetData(out total, out message, parameters);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View("ListDocuments");
            }
            //Success
            var sqlResult = data as List<LibDocuments> ?? null;
            ViewData["success"] = true;
            ViewData["total"] = total;
            return View("ListDocuments", sqlResult);
        }

        [AllowAnonymous]
        public ActionResult GetByViewCount()
        {
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@PageSize", 5);
            parameters.Add("@PageIndex", 1);
            parameters.Add("@OrderBy", "ViewCount");
            var data = _unitOfWork.LibDocuments.GetData(out total, out message, parameters);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View("ListDocumentsViewCount");
            }
            //Success
            var sqlResult = data as List<LibDocuments> ?? null;
            ViewData["success"] = true;
            ViewData["total"] = total;
            return View("ListDocumentsViewCount", sqlResult);
        }

        [AllowAnonymous]
        public ActionResult ListNew()
        {
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@PageSize", 5);
            parameters.Add("@PageIndex", 1);
            parameters.Add("@OrderBy", "CreateDate");
            var data = _unitOfWork.LibDocuments.GetData(out total, out message, parameters);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View("ListDocuments");
            }
            //Success
            var sqlResult = data as List<LibDocuments> ?? null;
            ViewData["success"] = true;
            ViewData["total"] = total;
            return View("ListDocuments", sqlResult);
        }

        [AllowAnonymous]
        public ActionResult GetByCategory(int categoryId)
        {
            ViewData["categoryId"] = categoryId;
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@CategoryId", categoryId);
            parameters.Add("@PageSize", 5);
            parameters.Add("@PageIndex", 1);
            parameters.Add("@OrderBy", "CreateDate");
            var data = _unitOfWork.LibDocuments.QuerySp(out total, out message, StringConstant.TABLE_LIBDOCUMENTS + StringConstant.SP_LIBDOCUMENTS_GETBYCATEGORYID, parameters);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View("ListDocumentsByCategory");
            }
            //Success
            var sqlResult = data as List<LibDocuments> ?? null;
            ViewData["success"] = true;
            ViewData["total"] = total;
            return View("ListDocumentsByCategory", sqlResult);
        }

        [AllowAnonymous]
        public ActionResult DocumentDetail(int documentId)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@DocumentId", documentId);
            parameters.Add("@Current", dbType: System.Data.DbType.Int32, direction: System.Data.ParameterDirection.Output);
            var data = _unitOfWork.LibDocuments.GetById(out message, parameters);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View("DocumentDetail");
            }
            //Success
            ViewData["success"] = true;
            ViewData["TotalDocument"] = parameters.Get<int>("@Current");
            return View("DocumentDetail", data);
        }

        [AllowAnonymous]
        public JsonResult SearchTop10By1Condition(string search)
        {
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@SearchCondition", search);
            var data = _unitOfWork.LibDocuments.QuerySpNT(out message, StringConstant.TABLE_LIBDOCUMENTS + StringConstant.SP_LIBDOCUMENTS_SEARCHTOP10BY1CONDITION, parameters);
            if (data == null)//Query error
            {
                return null;
            }
            //Success
            var sqlResult = data as List<LibDocuments> ?? null;
            return Json(sqlResult, JsonRequestBehavior.AllowGet);
        }

        [AllowAnonymous]
        public ActionResult SearchBy1Condition(string search)
        {
            if (search != null && !string.IsNullOrWhiteSpace(search))
            {
                ViewData["searchKey"] = search;
                int? total;
                string message;
                var parameters = new DynamicParameters();
                parameters.Add("@PageSize", 5);
                parameters.Add("@PageIndex", 1);
                parameters.Add("@SearchCondition", search.Trim());
                var data = _unitOfWork.LibDocuments.QuerySp(out total, out message, StringConstant.TABLE_LIBDOCUMENTS + StringConstant.SP_LIBDOCUMENTS_SEARCHONECONDITION, parameters);
                if (data == null)//Query error
                {
                    ViewData["success"] = false;
                    ViewData["message"] = message;
                    return View("SearchPage");
                }
                //Success
                var sqlResult = data as List<LibDocuments> ?? null;
                ViewData["success"] = true;
                ViewData["total"] = total;
                return View("SearchPage", sqlResult);
            }
            else
            {
                return View("Index");
            }
        }

        [Authorize(Roles = "System Admin, Lib Admin")]
        public ActionResult GetDataForAdmin()
        {
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@PageSize", 10);
            parameters.Add("@PageIndex", 1);
            parameters.Add("@OrderBy", "CreateDate");
            var data = _unitOfWork.LibDocuments.GetData(out total, out message, parameters);
            if (data == null)//Query error
            {
                ViewData["success"] = false;
                ViewData["message"] = message;
                return View("AdminListDocument");
            }
            //Success
            var sqlResult = data as List<LibDocuments> ?? null;
            ViewData["success"] = true;
            ViewData["total"] = total;
            return View("AdminListDocument", sqlResult);
        }

        [Authorize]
        [HttpGet]
        public ActionResult Streaming(string fileName, string ext)
        {
            return File(System.Configuration.ConfigurationManager.AppSettings["BaseDataFolder"] + fileName, "video/mp4");
        }

        [Authorize(Roles = "System Admin, Lib Admin")]
        public ActionResult UploadFile(int documentId, bool isModal)
        {
            ViewData["IsModal"] = isModal;
            ViewData["DocumentId"] = documentId;
            return View();
        }
    }
}
